package net.guoyixian.admin.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.alibaba.fastjson.JSON;

@Controller
public class BaseController {
	@ModelAttribute("jsRoot")
	public String jsRoot() {
//		return "http://localhost:8080/gyxshop-admin";
		return "http://localhost:8080";
	}

	@ModelAttribute("cssRoot")
	public String cssRoot() {
//		return "http://localhost:8080/gyxshop-admin";
		return "http://localhost:8080";
	}

	@ModelAttribute("imgRoot")
	public String imgRoot() {
//		return "http://localhost:8080/gyxshop-admin";
		return "http://localhost:8080";
	}

	/**
	 * 获取当前用户的id，从shiro session中获取
	 * 
	 * @return
	 */
	public int getCurrentUserId() {
		Subject shiroUser = SecurityUtils.getSubject();
		return (int) shiroUser.getSession().getAttribute("currentUserId");
	}

	/**
	 * 获取当前用户的username
	 * 
	 * @return
	 */
	@RequiresAuthentication
	public String getCurrentUserName() {
		Subject shiroUser = SecurityUtils.getSubject();
		return (String) shiroUser.getSession().getAttribute("currentUserName");
	}

	/**
	 * 获取当前session 的host
	 * 
	 * @return
	 */
	@RequiresAuthentication
	public String getHost() {
		Subject shiroUser = SecurityUtils.getSubject();
		return shiroUser.getSession().getHost();
	}
	
	/**
	 * 将对象转换成JSON字符串，并响应回前台
	 * 
	 * @param object
	 * @throws IOException
	 */
	public String writeJson(Object object) {
		String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
		return json;
	}

}